package com.haima.ziliao.controller;

import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;

import com.haima.common.core.redis.RedisCache;
import com.haima.common.utils.AliyunOssUploadUtils;
import com.haima.system.service.ISysConfigService;
import com.haima.utils.WxTool;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.haima.common.annotation.Log;
import com.haima.common.core.controller.BaseController;
import com.haima.common.core.domain.AjaxResult;
import com.haima.common.enums.BusinessType;
import com.haima.ziliao.domain.PZiliao;
import com.haima.ziliao.service.IPZiliaoService;
import com.haima.common.utils.poi.ExcelUtil;
import com.haima.common.core.page.TableDataInfo;

/**
 * 资料管理Controller
 * 
 * @author hima
 * @date 2024-10-21
 */
@RestController
@RequestMapping("/ziliao/ziliao")
public class PZiliaoController extends BaseController
{
    @Autowired
    private IPZiliaoService pZiliaoService;
    @Autowired
    private RedisCache redisCacheService;

    /**
     * 查询资料管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PZiliao pZiliao)
    {
        startPage();
        List<PZiliao> list = pZiliaoService.selectPZiliaoList(pZiliao);
        return getDataTable(list);
    }

    @GetMapping("/getZlLink")
    public AjaxResult getZlLink(PZiliao pZiliao)
    {
        PZiliao ziliao = pZiliaoService.selectPZiliaoById(pZiliao.getId());
        if(redisCacheService.hasKey("ziliao"+ziliao.getId())){
            return AjaxResult.success("ok",redisCacheService.getCacheObject("ziliao"+ziliao.getId()));
        }
        String page = "pages/zldetail/index?id="+ziliao.getId();
        String title = ziliao.getTitle();

        String link = WxTool.getShortLink(page,title);

        String copyLink = "名称："+title +"\n"+"链接："+link;
        redisCacheService.setCacheObject("ziliao"+ziliao.getId(),copyLink,29, TimeUnit.DAYS);
        return AjaxResult.success("ok",copyLink);
    }

    @GetMapping("/shua")
    public AjaxResult shua(PZiliao pZiliao)
    {
        List<PZiliao> list = pZiliaoService.selectPZiliaoListShua(pZiliao);
        return AjaxResult.success(list);
    }
    @GetMapping("/getDetail")
    public AjaxResult getDetail(PZiliao pZiliao)
    {
        PZiliao ziliao = pZiliaoService.selectPZiliaoById(pZiliao.getId());
        return AjaxResult.success(ziliao);
    }

    /**
     * 导出资料管理列表
     */
    @PreAuthorize("@ss.hasPermi('ziliao:ziliao:export')")
    @Log(title = "资料管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PZiliao pZiliao)
    {
        List<PZiliao> list = pZiliaoService.selectPZiliaoList(pZiliao);
        ExcelUtil<PZiliao> util = new ExcelUtil<PZiliao>(PZiliao.class);
        util.exportExcel(response, list, "资料管理数据");
    }

    /**
     * 获取资料管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ziliao:ziliao:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pZiliaoService.selectPZiliaoById(id));
    }

    /**
     * 新增资料管理
     */
    @PreAuthorize("@ss.hasPermi('ziliao:ziliao:add')")
    @Log(title = "资料管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PZiliao pZiliao)
    {
        return toAjax(pZiliaoService.insertPZiliao(pZiliao));
    }

    /**
     * 修改资料管理
     */
    @PreAuthorize("@ss.hasPermi('ziliao:ziliao:edit')")
    @Log(title = "资料管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PZiliao pZiliao)
    {
        return toAjax(pZiliaoService.updatePZiliao(pZiliao));
    }

    /**
     * 删除资料管理
     */
    @PreAuthorize("@ss.hasPermi('ziliao:ziliao:remove')")
    @Log(title = "资料管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        PZiliao pZiliao = pZiliaoService.selectPZiliaoById(ids[0]);
        AliyunOssUploadUtils.deleteFile(pZiliao.getPath());
        return toAjax(pZiliaoService.deletePZiliaoByIds(ids));
    }
}
